﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2011/10/27 22:09:46
 *      Version:        1.0.1101
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Sector7.Framework.Utility
{
    /// <summary>
    /// 正则表达式工具
    /// </summary>
    public class RegularUtil
    {
        #region Inner members
        /// <summary>
        /// 规则1：数字
        /// </summary>
        private const string NUMBER = @"^[\d]+$";

        /// <summary>
        /// 规则2：数字、点组合
        /// </summary>
        private const string NUMBER_POINT = @"^([\d]+\.)$";

        /// <summary>
        /// 规则3：小数
        /// </summary>
        private const string DECIMAL = @"^([\d]+\.[\d]+)$";

        /// <summary>
        /// 规则4：字母
        /// </summary>
        private const string LETTER = @"^[a-zA-Z]+$";

        /// <summary>
        /// 规则5：数字、字母组合
        /// </summary>
        private const string NUMBER_LETTER = @"^[a-zA-Z0-9]+$";

        /// <summary>
        /// 规则6：以字母或下划线开头。包括数字，字母与一般符号
        /// </summary>
        private const string NUMBER_LETTER_SYMBOL = @"((^[a-zA-Z]+[^\\/:*?""<>|][\d]*)|(^[_]+[^\\/:*?""<>|][a-zA-Z0-9]*))$";

        /// <summary>
        /// 规则7：非空匹配。包含字符串，日期等
        /// </summary>
        private const string NULL = @"^[\s]*$";

        /// <summary>
        /// 规则8：IP合法性检测
        /// </summary>
        private const string IP = @"^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]))$";

        /// <summary>
        ///规则9： 判断是否含有非法字符
        /// </summary>
        private const string ILLEGAL_LETTER = @"[\\/:*?""<>|\s]+";

        /// <summary>
        /// 规则10：判断各类名字命名（字母、数字、下划线、空格）
        /// </summary>
        private const string NAME_VALIDATE = @"^[a-zA-Z0-9_\s]*$";

        /// <summary>
        /// 规则11：判断整数
        /// </summary>
        private const string INTERGER = @"^[0-9]+$";
        #endregion

        #region Public functions
        /// <summary>
        /// 判断数字
        /// </summary>
        /// <returns>返回匹配成功与否</returns>
        public static bool IsNumber(object obj)
        {
            string str = obj.ToString().Trim();
            return Regex.IsMatch(str, NUMBER);
        }

        /// <summary>
        /// 判断数字、点组合
        /// </summary>
        /// <returns>返回匹配成功与否</returns>
        public static bool IsNumberPoint(object obj)
        {
            string str = obj.ToString().Trim();
            return Regex.IsMatch(str, NUMBER_POINT);
        }

        /// <summary>
        /// 判断小数
        /// </summary>
        /// <returns>返回匹配成功与否</returns>
        public static bool IsDecimal(object obj)
        {
            string str = obj.ToString().Trim();
            return Regex.IsMatch(str, DECIMAL);
        }

        /// <summary>
        /// 判断字母
        /// </summary>
        /// <returns>返回匹配成功与否</returns>
        public static bool IsLetter(object obj)
        {
            string str = obj.ToString().Trim();
            return Regex.IsMatch(str, LETTER);
        }

        /// <summary>
        /// 判断数字、字母组合
        /// </summary>
        /// <returns>返回匹配成功与否</returns>
        public static bool IsNumberLetter(object obj)
        {
            string str = obj.ToString().Trim();
            return Regex.IsMatch(str, NUMBER_LETTER);
        }

        /// <summary>
        /// 判断以字母或下划线开头。包括数字，字母与一般符号
        /// </summary>
        /// <returns>返回匹配成功与否</returns>
        public static bool IsNumberLetterSymbol(object obj)
        {
            string str = obj.ToString().Trim();
            return Regex.IsMatch(str, NUMBER_LETTER_SYMBOL);
        }

        /// <summary>
        /// 判断空
        /// </summary>
        /// <returns>返回匹配成功与否</returns>
        public static bool IsNull(object obj)
        {
            string str = obj.ToString().Trim();
            return Regex.IsMatch(str, NULL);
        }

        /// <summary>
        /// 判断IP地址
        /// </summary>
        /// <returns>返回匹配成功与否</returns>
        public static bool IsIPAddress(object obj)
        {
            string str = obj.ToString().Trim();
            return Regex.IsMatch(str, IP);
        }

        /// <summary>
        /// 判断非法字符（\/:*?"<>|）
        /// </summary>
        /// <returns>返回匹配成功与否</returns>
        public static bool IsIllegalWord(object obj)
        {
            string str = obj.ToString().Trim();
            return Regex.IsMatch(str, ILLEGAL_LETTER);
        }

        /// <summary>
        /// 判断各类名字命令规则（字母、数字、下划线、空格）
        /// </summary>
        /// <returns>返回匹配成功与否</returns>
        public static bool IsNameValidate(object obj)
        {
            string str = obj.ToString().Trim();
            return Regex.IsMatch(str, NAME_VALIDATE);
        }

        /// <summary>
        /// 判断整数
        /// </summary>
        /// <param name="obj">待判断值</param>
        /// <returns></returns>
        public static bool IsInteger(object obj)
        {
            string str = obj.ToString().Trim();
            return Regex.IsMatch(str, INTERGER);
        }
        #endregion
    }
}
